<?php
class ClienDAO{
	var $db;

	function __construct($db){
		$this->db = $db;
	}

	function setDb($db){
		$this->db = $db;
	}

	function getDb(){
		return $this->db;
	}

	function addClien($c){
		$sql = "insert into clien (nome, email, tel1, tel2, cpf, ativo) 
				values (".($c->getNome() ? "'{$c->getNome()}'" : "null")."','{$c->getEmail()}','{$c->getTel1()}','{$c->getTel2()}','{$c->getCpf()}','{$c->getAtivo()}')";
		$res = $this->getDb()->query($sql);
		return $res;		
	}

	function setClien($c){
		$sql = "update clien
				set nome = ".($c->getNome() ? "'{$c->getNome()}'" : "null")."',
					email = '{$c->getEmail()}',
					tel1 = '{$c->getTel1()}',
					tel2 = '{$c->getTel2()}',
					cpf = '{$c->getCpf()}',
					ativo = '{$c->getAtivo()}'
				where clien = {$c->getId()}";
		$res = $this->getDb()->query($sql);
		return $res;		
	}

	function getClien($id){
		$sql = "select CLIEN, NOME, EMAIL, TEL1, TEL2, CPF, ATIVO
				from clien
				where clien = {$id}";
		$res = $this->getDb()->query($sql);

		if($res){
			$r = $res[0];
			$c = new Clien($r['CLIEN'],$r['NOME'],$r['EMAIL'],$r['CPF'],$r['TEL1'],$r['TEL2'],$r['ATIVO']);
			return $c;
		}
		return false;
	}

	function getLista($where=array()){
		$lista = array();

		$where = ($where ? "where ".implode(" and ", $where) : "");
		$sql = "select CLIEN, NOME, EMAIL, TEL1, TEL2, CPF, ATIVO
				from clien
				{$where}";
		$res = $this->getDb()->query($sql);
		if($res){
			foreach($res as $r){
				$lista[] = new Clien($r['CLIEN'],$r['NOME'],$r['EMAIL'],$r['CPF'],$r['TEL1'],$r['TEL2'],$r['ATIVO']);
			}
		}

		return $lista;
	}	

	function delClien(){
	}
}
?>